package lg.minecraft.plugin.pokemonmenu.listener;

import com.cobblemon.mod.common.api.events.battles.BattleFledEvent;
import com.cobblemon.mod.common.api.events.battles.BattleVictoryEvent;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lg.minecraft.plugin.pokemonmenu.PokemonPlayer;
import lg.minecraft.plugin.pokemonmenu.event.PlayerEndBattleHealEvent;
import lg.minecraft.plugin.pokemonmenu.manager.PokemonPlayerManger;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.UUID;

/**
 * @author langle__
 * @version 1.0
 */
public class CobblemonEvent {

    public static void onBattleVictory(BattleVictoryEvent event) {
        for (UUID uuid : event.getBattle().getPlayerUUIDs()) {
            Player player = Bukkit.getPlayer(uuid);
            if (player == null) continue;
            PokemonPlayer pokemonPlayer = PokemonPlayerManger.INSTANCE.getPokemonPlayer(player);
            if (pokemonPlayer.isAutoHeal()) {
                Bukkit.getScheduler().runTaskLater(LGPokemonMenu.getInstance(), () -> Bukkit.getPluginManager().callEvent(new PlayerEndBattleHealEvent(player)), 5L);
            }
        }
    }

    public static void onBattleFled(BattleFledEvent event) {
        for (UUID uuid : event.getBattle().getPlayerUUIDs()) {
            Player player = Bukkit.getPlayer(uuid);
            if (player == null) continue;
            PokemonPlayer pokemonPlayer = PokemonPlayerManger.INSTANCE.getPokemonPlayer(player);
            if (pokemonPlayer.isAutoHeal()) {
                Bukkit.getPluginManager().callEvent(new PlayerEndBattleHealEvent(player));
            }
        }
    }

}
